<template>
  <ComWrapper>
    <UploadExcel :on-success="handleSuccess" :before-upload="beforeUpload" />
    <Table :tableData="tableData" style="margin-top:20px;">
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
    </Table>
  </ComWrapper>
</template>

<script setup>
import {ref, defineAsyncComponent} from 'vue'
import {ElMessage} from 'element-plus'
import UploadExcel from '@/components/UploadExcel'

const tableData = ref([])
const tableHeader = ref([])

const Table = defineAsyncComponent(() => import('@/components/Table'))

function beforeUpload(file) {
  const isLt1M = file.size / 1024 / 1024 < 1
  if (isLt1M) {
    return true
  }
  ElMessage({
    message: 'Please do not upload files larger than 1m in size.',
    type: 'warning'
  })
  return false
}
function handleSuccess({results, header}) {
  tableData.value = results
  tableHeader.value = header
}
</script>
